package chapter18;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2019-03-31 21:34
 */
public abstract class Shape implements Serializable {
    public static final int RED = 1, BLUE = 2, GREEN = 3;
    private static Random random = new Random(47);
    private static int counter = 0;
    private int xPos, yPos, dimension;

    public Shape(int xPos, int yPos, int dimension) {
        this.xPos = xPos;
        this.yPos = yPos;
        this.dimension = dimension;
    }

    public static Shape randomFactory() {
        int xVal = random.nextInt(100);
        int yVal = random.nextInt(100);
        int dim = random.nextInt(100);
        switch (counter++ % 3) {
            default:
            case 0:
                return new Circle(xVal, yVal, dim);
            case 1:
                return new Square(xVal, yVal, dim);
            case 2:
                return new Line(xVal, yVal, dim);
        }
    }

    public abstract int getColor();

    public abstract void setColor(int newColor);

    @Override
    public String toString() {
        return "Shape{" +
                "xPos=" + xPos +
                ", yPos=" + yPos +
                ", dimension=" + dimension +
                ", color=" + getColor() +
                '}' + '\n';
    }
}

class Circle extends Shape {

    private static int color = RED;

    public Circle(int xPos, int yPos, int dimension) {
        super(xPos, yPos, dimension);
    }

    public static void serializeStaticState(ObjectOutputStream out)
            throws IOException {
        out.writeInt(color);
    }

    public static void deserializeStaticState(ObjectInputStream in)
            throws IOException {
        color = in.readInt();
    }

    @Override
    public int getColor() {
        return color;
    }

    @Override
    public void setColor(int newColor) {
        color = newColor;
    }
}

class Square extends Shape {

    private static int color;

    public Square(int xPos, int yPos, int dimension) {
        super(xPos, yPos, dimension);
        color = RED;
    }

    public static void serializeStaticState(ObjectOutputStream out)
            throws IOException {
        out.writeInt(color);
    }

    public static void deserializeStaticState(ObjectInputStream in)
            throws IOException {
        color = in.readInt();
    }

    @Override
    public int getColor() {
        return color;
    }

    @Override
    public void setColor(int newColor) {
        color = newColor;
    }
}

class Line extends Shape {

    private static int color = RED;

    public Line(int xPos, int yPos, int dimension) {
        super(xPos, yPos, dimension);
    }

    public static void serializeStaticState(ObjectOutputStream out)
            throws IOException {
        out.writeInt(color);
    }

    public static void deserializeStaticState(ObjectInputStream in)
            throws IOException {
        color = in.readInt();
    }

    @Override
    public int getColor() {
        return color;
    }

    @Override
    public void setColor(int newColor) {
        color = newColor;
    }
}
